package com.fawvwebanma.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fawvwebanma.api.PayApi;
import com.fawvwebanma.dao.OrderListDao;
import com.fawvwebanma.dao.PayDao;
import com.fawvwebanma.dao.TicketDao;
import com.fawvwebanma.dto.Result;
import com.fawvwebanma.entity.OrderList;
import com.fawvwebanma.entity.Pay;
import com.fawvwebanma.entity.Ticket;
import com.fawvwebanma.service.IPayService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jieye.ren
 * @date 2021/3/12
 */
@Component
@DubboService
@Slf4j
public class PayService implements IPayService {
    @DubboReference(version = "${dubbo.provider.version}", check = false, application = "${dubbo.application.id}")
    PayApi payApi;
    @Autowired
    PayDao payDao;
    @Autowired
    OrderListDao orderListDao;
    @Autowired
    TicketDao ticketDao;

    @Override
    public Result<Object> payOrderlist(String orderNum) {
        if (payApi.pay(orderNum).isSuccess()) {
            // pay表插入数据
            Pay pay = new Pay();
            pay.setOrderNum(orderNum);
            pay.setPayStatus(1);
            pay.setPayId("8768987609898976");
            pay.setPayTime(new Date());
            payDao.save(pay);
            // orderlist表更新订单状态
            OrderList orderList = new OrderList();
            orderList.setOrderNum(orderNum);
            orderList.setOrderStatus(1);
            orderListDao.updateOrderStatus(orderList);
            // ticket表更新status
            Ticket ticket = new Ticket();

            Map<String, Object> map = new HashMap<>();
            map.put("order_num", orderNum);
            ticket.setStatus(1);
            ticketDao.updateTicketStatus(ticket, map);

            return Result.success();
        } else {
            return Result.error("0", "支付失败...");
        }

    }
}
